/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.ctop.modules.job.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ctop.common.config.Global;
import com.ctop.common.persistence.Page;
import com.ctop.common.web.BaseController;
import com.ctop.common.utils.StringUtils;
import com.ctop.modules.job.entity.ScheduleJob;
import com.ctop.modules.job.service.ScheduleJobService;

/**
 * 定时任务日志Controller
 * @author linbc
 * @version 2017-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/job/scheduleJob")
public class ScheduleJobController extends BaseController {

	@Autowired
	private ScheduleJobService scheduleJobService;
	
	@ModelAttribute
	public ScheduleJob get(@RequestParam(required=false) String id) {
		ScheduleJob entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = scheduleJobService.get(id);
		}
		if (entity == null){
			entity = new ScheduleJob();
		}
		return entity;
	}
	
	@RequiresPermissions("job:scheduleJob:view")
	@RequestMapping(value = {"list", ""})
	public String list(ScheduleJob scheduleJob, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ScheduleJob> page = scheduleJobService.findPage(new Page<ScheduleJob>(request, response), scheduleJob); 
		model.addAttribute("page", page);
		return "modules/job/scheduleJobList";
	}

	@RequiresPermissions("job:scheduleJob:view")
	@RequestMapping(value = "form")
	public String form(ScheduleJob scheduleJob, Model model) {
		model.addAttribute("scheduleJob", scheduleJob);
		return "modules/job/scheduleJobForm";
	}

	@RequiresPermissions("job:scheduleJob:edit")
	@RequestMapping(value = "save")
	public String save(ScheduleJob scheduleJob, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, scheduleJob)){
			return form(scheduleJob, model);
		}
		scheduleJobService.save(scheduleJob);
		addMessage(redirectAttributes, "保存定时任务日志成功");
		return "redirect:"+Global.getAdminPath()+"/job/scheduleJob/?repage";
	}
	
	@RequiresPermissions("job:scheduleJob:edit")
	@RequestMapping(value = "delete")
	public String delete(ScheduleJob scheduleJob, RedirectAttributes redirectAttributes) {
		scheduleJobService.delete(scheduleJob);
		addMessage(redirectAttributes, "删除定时任务日志成功");
		return "redirect:"+Global.getAdminPath()+"/job/scheduleJob/list";
	}
	@RequiresPermissions("job:scheduleJob:edit")
	@RequestMapping(value = "pause")
	public String pause(ScheduleJob scheduleJob, RedirectAttributes redirectAttributes) {
		scheduleJobService.pause(scheduleJob);
		addMessage(redirectAttributes, "暂停定时任务日志成功");
		return "redirect:"+Global.getAdminPath()+"/job/scheduleJob/list";
	}
	@RequiresPermissions("job:scheduleJob:edit")
	@RequestMapping(value = "resume")
	public String resume(ScheduleJob scheduleJob, RedirectAttributes redirectAttributes) {
		scheduleJobService.resume(scheduleJob);
		addMessage(redirectAttributes, "恢复定时任务日志成功");
		return "redirect:"+Global.getAdminPath()+"/job/scheduleJob/list";
	}
	@RequiresPermissions("job:scheduleJob:edit")
	@RequestMapping(value = "run")
	public String run(ScheduleJob scheduleJob, RedirectAttributes redirectAttributes) {
		scheduleJobService.run(scheduleJob);
		addMessage(redirectAttributes, "立即执行定时任务日志成功");
		return "redirect:"+Global.getAdminPath()+"/job/scheduleJob/list";
	}

}